
class GimytvInfo extends AbstractPlugin {

  static BASE_URL = 'https://gimytv.io';

  async fetchWebsiteInfo() {
    const url = window.location.href;

    const parser = new DOMParser();
    const doc = parser.parseFromString(document.body.outerHTML, 'text/html');

    const playSources = [];

    let episodes = [];
    const uniqueLinks = new Set();
    const items = doc.querySelectorAll('.playlist a');
    items.forEach(item => {
      const episodeItem = new Episode();
      episodeItem.title = item.textContent;
      const href = item.getAttribute('href');
      episodeItem.link = href.startsWith('/') ? `${GimytvInfo.BASE_URL}${href}` : href;
      if(!uniqueLinks.has(episodeItem.link)) {
        uniqueLinks.add(episodeItem.link);
        episodes.push(episodeItem);
      }
    });
    if(episodes.length === 0) {
      return null;
    }
    // episodes.reverse();
    playSources.push(new Source({
      title: '劇迷',
      episodes: episodes
    }));

    const websiteInfo = new WebsiteInfo();
    websiteInfo.type = 'video';
    let titleElement = doc.querySelector('.details-info h1');
    if(!titleElement) {
      titleElement = doc.querySelector('.player_title2 a');
    }
    if(titleElement) {
      websiteInfo.title = titleElement.textContent.trim();
    }
    websiteInfo.link = url;
    websiteInfo.sources = playSources;

    const playRegex = /\/eps\//;
    if(playRegex.test(url)) {
      websiteInfo.playUrl = await this.getPlayUrlFromBody(document.body.outerHTML);
      const curEp = doc.querySelector('.playlist a.active');
      if(curEp) {
        const href = curEp.getAttribute('href');
        const curEpLink = href.startsWith('/') ? `${GimytvInfo.BASE_URL}${href}` : href;
        [websiteInfo.sourceIndex, websiteInfo.episodeIndex] = AbstractPlugin.findEpisode(playSources, curEpLink);
      }
    }
    return websiteInfo;
  }

  async fetchPlayUrlInfo(url) {
    const http_headers = {
      'User-Agent': GimytvInfo.DEFAULT_USER_AGENT,
      'Origin': GimytvInfo.BASE_URL,
    };
    const result = await doHttpRequestSync(url, { headers: http_headers });
    if(result.status !== 'success') {
      return null;
    }
    return this.getPlayUrlFromBody(result.body)
  }

  async getPlayUrlFromBody(body) {
    const iframeMatch = body.match(/<iframe[^>]+src="([^"]+)"/);
    if(!iframeMatch) return null;

    // TODO: 多个源的情况, 是否需要解析
    // const sourcesElements = doc.querySelectorAll('.details-play-title a');
    const infoUrl = GimytvInfo.BASE_URL + iframeMatch[1];
    const http_headers = {
      'User-Agent': GimytvInfo.DEFAULT_USER_AGENT,
      'Origin': GimytvInfo.BASE_URL,
    };
    const result = await doHttpRequestSync(infoUrl, { headers: http_headers });
    if(result.status !== 'success') {
      return null;
    }
    const urlMatch = result.body.match(/var url = '([^']+)'/);
    if(!urlMatch) {
      return null;
    }
    const playUrl = new PlayUrlInfo();
    playUrl.type = 'video';
    playUrl.urls = [urlMatch[1]];
    return playUrl;
  }

}
Registry.registerClass("GimytvInfo", new GimytvInfo());